package com.heima.kafka.controller;

import com.alibaba.fastjson.JSON;
import com.heima.kafka.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author ItZheng
 * @Date 2023/6/17 16:46
 * @Version 1.5
 */
@RestController
public class KafkaController {
    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    @GetMapping("/hello")
    public String hello() {
        //kafkaTemplate.send("itcast-topic", "黑马程序员");

        //发送消息为对象：
        User user = new User();
        user.setName("xiaowang");
        user.setAge(18);
        //需要将对象 使用 JSON 转为 json 字符串进行发送
        kafkaTemplate.send("user-topic", JSON.toJSONString(user));

        return "ok";
    }
}
